package countDownLatchTest;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

/**
 * @author zxs
 * @create 2021-03-31 16:55
 */
public class StuThread implements Runnable {
    private long time;
    private CountDownLatch countDownLatch;
    private Semaphore semaphore;

    public StuThread(long time, CountDownLatch countDownLatch,Semaphore semaphore) {
        this.countDownLatch = countDownLatch;
        this.time = time;
        this.semaphore = semaphore;
    }

    @Override
    public void run() {
        try {
            semaphore.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "完成了考试,用时：" + time);
        semaphore.release();
        countDownLatch.countDown();
    }
}
